package com.longge;

/**
 * @创建人 xinglongge
 * @创建时间 2021/10/14
 * @描述
 */
public class MajorityElement {
    public int majorityElement(int[] nums) {

        int cur = nums[0], count = 1;
        for (int i = 1; i < nums.length; i++) {
            if (count == 0) {
                cur = nums[i];
                count++;
                continue;
            }
            if (nums[i] == cur) count++;
            else count--;
        }
        count = 0;
        for (int num : nums) {
            if (num == cur) {
                count++;
            }
        }
        return count * 2 > nums.length ? cur : -1;
    }
}
